package ui;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import Work.Jdbc;

public class DengLu extends JFrame implements ActionListener {

	JTextField jtf;
	JPasswordField jpf;
	public String s1;
	// 创建数据库连接类对象
	Jdbc db = new Jdbc();
	public DengLu() {
		JPanel jpc = new JPanel();// 默认流式布局
		JPanel jp1 = new JPanel(new GridLayout(2, 2, 5, 10));// 设置新面板
		// 网格布局GridLayout(int rows, int columns, int hgap, int vgap)行数，列数，组件水平间距，垂直间距
		jp1.setBorder(BorderFactory.createTitledBorder("读者登录"));
		JLabel jl1 = new JLabel("读者编号:");
		jtf = new JTextField(10);
		s1 = jtf.getText();
		JLabel jl2 = new JLabel("密      码:");
		jpf = new JPasswordField(10);
		jpf.setEchoChar('*');// 用*号来隐藏密码的显示

		jp1.add(jl1);// 在面板jp1上添加标签jl1
		jp1.add(jtf);// 在面板jp1上添加输入框

		jp1.add(jl2);
		jp1.add(jpf);

		jpc.add(jp1);// 把面板jp1加到流式布局上

		add(jpc);// 添加流式布局

		JButton jb1 = new JButton("提交");
		jb1.addActionListener(this);
		jb1.setActionCommand("yes");// 没看懂

		JButton jb2 = new JButton("清空");
		jb2.addActionListener(this);
		jb2.setActionCommand("no");

		JButton jb3 = new JButton("返回");
		jb3.addActionListener(this);
		jb3.setActionCommand("back");

		JPanel jp2 = new JPanel();// 新面板，用于放置按钮
		jp2.add(jb1);
		jp2.add(jb2);
		jp2.add(jb3);
		add(jp2, BorderLayout.SOUTH);// 把按钮放到窗口最下方

		setTitle("读者登录界面");
		setSize(280, 160);
		setLocationRelativeTo(null);// 窗口居中
		// 组件为空，或者与此组件关联的图形配置为空，则窗口将位于屏幕的中心。
		setDefaultCloseOperation(EXIT_ON_CLOSE);//
		setVisible(true);
	}

	@Override

	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("yes")) {
			String name = jtf.getText().trim();
			String pwd = new String(jpf.getPassword());
			String a = db.uifindLogin1(db,name,"1");
			System.out.print(a);
			if (name.equals("") || pwd.equals("")) {
				JOptionPane.showMessageDialog(this, "你还没有输入用户名或者密码");
			}else if(pwd.equals(db.uifindLogin1(db,name,"1"))){
				JOptionPane.showMessageDialog(this, "登录成功!用户名" + name + ",密码" + pwd);
				System.out.println(s1);
				this.dispose();
				new ReaderOp();
			}else {
				JOptionPane.showMessageDialog(this, "登录失败！用户名/密码错误");
			}
		} else if (e.getActionCommand().equals("no")) {
			jtf.setText("");
			jpf.setText("");
		} else if (e.getActionCommand().equals("back")) {
			this.dispose();
			new Bind();
		}
	}
}